package com.hx.setting.dao.imp;

import com.hx.setting.dao.AbstractJDBCDaoSupport.ResultSetHandler;
import com.hx.setting.dao.BaseDao;
import com.hx.setting.dao.RoleDao;
import com.hx.setting.dao.imp.JdbcBaseDaoImp;
import com.hx.setting.entity.RoleEntity;
import com.hx.setting.model.Condition;
import com.hx.setting.model.GeneratedCriteria.Criterion;
import com.hx.setting.model.GeneratedCriteria;
import com.hx.setting.model.Page;
import com.hx.setting.model.RoleModel.ParentroleEnum;
import com.hx.setting.model.RoleModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Generate Date : 2014-03-16
 * Generate Version : v3.0
 * Generate Name : Honey v3.1 Test
 * Generate User : THINK
 * JDK Version : 1.5
 */
@SuppressWarnings("unchecked")
public class RoleDaoImp<E extends RoleEntity, PK extends Long> extends JdbcBaseDaoImp<E, PK> implements RoleDao<E, PK>
{

	private static final String		TABLE_NAME			= "hx_role";

	private static final String		PK_FIELD_NAME		= "id";

	private static final String		BASE_QUERY_FIELD	= "hx_role.id AS id ,hx_role.rolename AS rolename ,hx_role.parentrole AS parentrole ,hx_role.depth AS depth ,hx_role.allowassignedrecordsto AS allowassignedrecordsto ";

	private static final String[]	BASE_FIELD			= new String[]
														{ "id", "rolename", "parentrole", "depth", "allowassignedrecordsto" };

	private static final String		BLOB_QUERY_FIELD	= null;

	private static final String[]	BLOB_FIELD			= null;

	@Override
	protected String getTableName()
	{
		return TABLE_NAME;
	}

	@Override
	protected String getFieldPrimaryKeyName()
	{
		return PK_FIELD_NAME;
	}

	@Override
	protected ResultSetHandler getResultSetHandler()
	{
		return new RoleDaoImp.QueryResultSetHandler();
	}

	@Override
	protected String[] getBaseField()
	{
		return BASE_FIELD;
	}

	@Override
	protected String[] getBlobField()
	{
		return BLOB_FIELD;
	}

	@Override
	protected String getBaseQueryField()
	{
		return BASE_QUERY_FIELD;
	}

	@Override
	protected String getBlobQueryField()
	{
		return BLOB_QUERY_FIELD;
	}

	@Override
	protected E getEntity(boolean hasBlob)
	{
		RoleEntity answer = null;
		answer = new RoleEntity();
		return (E) answer;
	}

	@Override
	public int insert(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			sql.append( "( id, rolename, parentrole, depth, allowassignedrecordsto ) VALUES ( ?, ?, ?, ?, ? )" );
			Object[] params = new Object[]
			{ bean.getId(), bean.getRolename(), bean.getParentrole(), bean.getDepth(), bean.getAllowassignedrecordsto() };
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int insertSelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder( "INSERT INTO " ).append( TABLE_NAME );
			StringBuilder field = new StringBuilder();
			StringBuilder value = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getId();
			if ( o != null )
			{
				field.append( "id, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean.getRolename();
			if ( o != null )
			{
				field.append( "rolename, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean.getParentrole();
			if ( o != null )
			{
				field.append( "parentrole, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean.getDepth();
			if ( o != null )
			{
				field.append( "depth, " );
				value.append( "?, " );
				params.add( o );
			}
			o = bean.getAllowassignedrecordsto();
			if ( o != null )
			{
				field.append( "allowassignedrecordsto, " );
				value.append( "?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			sql.append( "( " ).append( field ).append( ") VALUES (" );
			value.deleteCharAt( value.length() - 2 );
			sql.append( value ).append( ')' );
			try
			{
				GeneratedKeysHandler keyHandler = new GeneratedKeysHandler();
				answer = update( prepareConnection() ,true ,sql.toString() ,keyHandler ,params.toArray() );
			}
			catch ( SQLException e )
			{
				e.printStackTrace();
			}
		}
		return answer;
	}

	@Override
	public int update(E bean)
	{
		return updateByPrimaryKey( bean );
	}

	@Override
	public int updateByPrimaryKey(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("id=? ,rolename=? ,parentrole=? ,depth=? ,allowassignedrecordsto=?  ");
			answer = update( set ,"id=? " ,new Object[]
			{ bean.getId(), bean.getRolename(), bean.getParentrole(), bean.getDepth(), bean.getAllowassignedrecordsto(), bean.getId() } );
		}
		return answer;
	}

	public int updateByPrimaryKeySelective(E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder sql = new StringBuilder();
			StringBuilder field = new StringBuilder();
			StringBuilder finalCond = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getId();
			if ( o != null )
			{
				field.append( "id=?, " );
				params.add( o );
			}
			o = bean.getRolename();
			if ( o != null )
			{
				field.append( "rolename=?, " );
				params.add( o );
			}
			o = bean.getParentrole();
			if ( o != null )
			{
				field.append( "parentrole=?, " );
				params.add( o );
				//finalCond.append(""  );
			}
			o = bean.getDepth();
			if ( o != null )
			{
				field.append( "depth=?, " );
				params.add( o );
			}
			o = bean.getAllowassignedrecordsto();
			if ( o != null )
			{
				field.append( "allowassignedrecordsto=?, " );
				params.add( o );
			}
			field.deleteCharAt( field.length() - 2 );
			params.add( bean.getId() );
			answer = update( field.toString() ,"id=? " ,params.toArray() );
		}
		return answer;
	}

	@Override
	public int save(E bean)
	{
		return 0;
	}

	@Override
	public int updateByDynamic(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			String set = ("id=? ,rolename=? ,parentrole=? ,depth=? ,allowassignedrecordsto=? ");
			Object[] build = make( search );
			answer = update( set ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( new Object[]
			{ bean.getId(), bean.getRolename(), bean.getParentrole(), bean.getDepth(), bean.getAllowassignedrecordsto() } ,(Object[]) build[1] ) );
		}
		return answer;
	}

	@Override
	public int updateByDynamicSelective(Condition<? extends GeneratedCriteria> search, E bean)
	{
		int answer = 0;
		if ( bean != null )
		{
			StringBuilder set = new StringBuilder();
			List params = new ArrayList();
			Object o = null;
			o = bean.getId();
			if ( o != null )
			{
				set.append( "id=?, " );
				params.add( o );
			}
			o = bean.getRolename();
			if ( o != null )
			{
				set.append( "rolename=?, " );
				params.add( o );
			}
			o = bean.getParentrole();
			if ( o != null )
			{
				set.append( "parentrole=?, " );
				params.add( o );
			}
			o = bean.getDepth();
			if ( o != null )
			{
				set.append( "depth=?, " );
				params.add( o );
			}
			o = bean.getAllowassignedrecordsto();
			if ( o != null )
			{
				set.append( "allowassignedrecordsto=?, " );
				params.add( o );
			}
			set.deleteCharAt( set.length() - 2 );
			Object[] build = make( search );
			answer = update( set.toString() ,(String) build[0] ,com.hx.setting.utils.StringUtils.join( params.toArray() ,(Object[]) build[1] ) );
		}
		return answer;
	}

	/** 
	 * 按照parentrole不带分页查询.
	 * @param state 值
	 * @return 返回查询列表
	 */
	@Override
	public List<E> selectByParentrole(ParentroleEnum parentrole)
	{
		List<E> answer = null;
		answer = super.selectByField( RoleModel.RoleTableField.PARENTROLE ,parentrole.getValue() ,new Page( 0, 1 ) );
		return answer;
	}

	/** 
	 * 按照parentrole带分页查询.
	 * @param state 值
	 * @param page 分页
	 * @return 返回查询列表
	 */
	@Override
	public List<E> selectByParentrole(ParentroleEnum parentrole, Page page)
	{
		List<E> answer = null;
		answer = super.selectByField( RoleModel.RoleTableField.PARENTROLE ,parentrole.getValue() ,page );
		return answer;
	}

	/** 
	 * 按照主键更新状态(parentrole)
	 * @param pk 主键
	 * @param parentrole 状态枚举
	 * @return 返回影响的记录集数量
	 */
	@Override
	public int updateByParentrole(PK pk, ParentroleEnum parentrole)
	{
		return update( "parentrole=? " ,"id=? " ,new Object[]
		{ parentrole.getValue(), pk } );
	}

	private class QueryResultSetHandler implements ResultSetHandler
	{

		@Override
		public RoleEntity handle(ResultSet resultSet, boolean hasBlob) throws SQLException
		{
			RoleEntity bean = null;
			if ( resultSet.next() )
			{
				bean = new RoleEntity();
				Object obj = null;
				obj = resultSet.getLong( "id" );
				if ( obj != null && !resultSet.wasNull() ) bean.setId( (Long) obj );
				obj = resultSet.getString( "rolename" );
				if ( obj != null && !resultSet.wasNull() ) bean.setRolename( (String) obj );
				obj = resultSet.getString( "parentrole" );
				if ( obj != null && !resultSet.wasNull() ) bean.setParentrole( ParentroleEnum.getInstance( (String) obj ) );
				obj = resultSet.getInt( "depth" );
				if ( obj != null && !resultSet.wasNull() ) bean.setDepth( (Integer) obj );
				obj = resultSet.getShort( "allowassignedrecordsto" );
				if ( obj != null && !resultSet.wasNull() ) bean.setAllowassignedrecordsto( (Short) obj );
			}
			return bean;
		}
	}
}
/* Honey v3.1 Test - version:4 - OriginalChecksum:B575b63f8b83e31b092c7ce68b50478c9 (do not edit this line) */
